package com.neuedu.web.service;

import com.neuedu.web.common.HisConstants;
import com.neuedu.web.dao.*;
import com.neuedu.web.po.*;
import com.neuedu.web.utils.ParamUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 金山
 * 项目：his-server
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/11/2911:25
 */
public class DoctorService {

    RegisterDao registerDao = new RegisterDao();

    CheckItemDao checkItemDao = new CheckItemDao();

    CheckApplyDao checkApplyDao = new CheckApplyDao();



    InspectItemDao inspectItemDao = new InspectItemDao();

    InspectApplyDao inspectApplyDao = new InspectApplyDao();

    /**
     *
     * @param request
     * @param response
     * @return
     */
    public List<RegisterPO> queryRegister(HttpServletRequest request , HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String type = request.getParameter("type");

        HttpSession session = request.getSession();
        UmsUser user = (UmsUser) session.getAttribute("user");

        // if(type.equals("doctor"))

        String sql = "";
        Object param = null;
        if("doctor".equals(type)){
            sql = "select * from register  where date(visit_time) = ? and userid = ?";
            param = user.getUserid();
        }else if("dept".equals(type)){
            sql = "select * from register  where  date(visit_time) =  ?  and   dept_id = ?";
            param = user.getDeptId();
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String now = simpleDateFormat.format(new Date());


        List<RegisterPO> registerList = registerDao.selectAll(sql, now, param);

       return  registerList;
    }


    /**
     * 病历首页保存， 现病史、疾病史、过敏史...
     * @param request
     * @param response
     * @return
     */
    public boolean inquiry(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        RegisterPO registerPO = ParamUtils.getParam(RegisterPO.class,request);


        Integer registId = registerPO.getRegistId();
        RegisterPO dbRegister = registerDao.selectById(registId.longValue());

        dbRegister.setReadme(registerPO.getReadme());
        dbRegister.setPresent(registerPO.getPresent());
        dbRegister.setPresentTreat(registerPO.getPresentTreat());
        dbRegister.setHistory(registerPO.getHistory());
        dbRegister.setAllergy(registerPO.getAllergy());


        //
        dbRegister.setStatus(registerPO.getStatus());


        int update = registerDao.update(dbRegister);

        return update > 0 ;
    }


    /**
     * 查询患者信息
     * @param request
     * @param response
     * @return
     */
    public RegisterPO queryRegisterInfo(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String id = request.getParameter("id");
        RegisterPO registerPO = registerDao.selectById(Long.parseLong(id));

        return registerPO;
    }



    public boolean checkApply(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        String  registId = request.getParameter("registId");
        String  strCheckIds = request.getParameter("checkIds");

        CheckApplyPO applyPO = new CheckApplyPO();
        applyPO.setRegistId(Integer.parseInt(registId));

        String[] checkIds = strCheckIds.split(",");

        for (String checkId : checkIds) {
            CheckitemPO checkitemPO = checkItemDao.selectById(Long.parseLong(checkId));

            applyPO.setCheckId(checkitemPO.getCheckId());
            applyPO.setName(checkitemPO.getName());
            applyPO.setPrice(checkitemPO.getPrice());
            //默认是已申请的状态
            applyPO.setStatus(HisConstants.CHECK_APPLAY_STATUS_1);

            checkApplyDao.insert(applyPO);
        }
        return true;
    }

    public List<CheckApplyPO> queryCheckApplyByRegisterId(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String  registerId = request.getParameter("registerId");

        return checkApplyDao.selectAll("select * from check_apply where regist_id = ?",registerId);

    }

    public boolean removeCheckApplayItem(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String  itemId = request.getParameter("itemId");

        return checkApplyDao.delete(Long.valueOf(itemId))>0;

    }




    public boolean inspectApply(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        String  registId = request.getParameter("registId");
        String  strInspectIds = request.getParameter("inspectIds");

        InspectApplyPO applyPO = new InspectApplyPO();
        applyPO.setRegistId(Integer.parseInt(registId));

        String[] inspectIds = strInspectIds.split(",");

        for (String inspectId : inspectIds) {
            InspectitemPO inspectitemPO = inspectItemDao.selectById(Long.parseLong(inspectId));

            applyPO.setInspectId(inspectitemPO.getInspectId());
            applyPO.setName(inspectitemPO.getName());
            applyPO.setPrice(inspectitemPO.getPrice());
            //默认是已申请的状态
            applyPO.setStatus(HisConstants.CHECK_APPLAY_STATUS_1);

            inspectApplyDao.insert(applyPO);
        }
        return true;
    }

    public List<InspectApplyPO> queryInspectApplyByRegisterId(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String  registerId = request.getParameter("registerId");

        return inspectApplyDao.selectAll("select * from inspect_apply where regist_id = ?",registerId);

    }

    public boolean removeInspectApplayItem(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String  itemId = request.getParameter("itemId");

        return inspectApplyDao.delete(Long.valueOf(itemId))>0;

    }


    public boolean sure(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        //页面发送 的  疾病、诊断 处置方案
        RegisterPO registerPO = ParamUtils.getParam(RegisterPO.class,request);


        Integer registId = registerPO.getRegistId();
        RegisterPO dbRegister = registerDao.selectById(registId.longValue());

        dbRegister.setDisease(registerPO.getDisease());
        dbRegister.setSuit(registerPO.getSuit());
        dbRegister.setDrug(registerPO.getDrug());



        // 已确诊
        dbRegister.setStatus(HisConstants.REGISTER_STATUS_SURE);


        int update = registerDao.update(dbRegister);

        return update > 0 ;
    }
}
